﻿package com.assessment.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.assessment.entity.Staff;
import com.assessment.service.StaffService;
import com.assessment.util.FileUpload;
import com.assessment.util.importStaff;


@Controller
@RequestMapping("/staff")
public class StaffController {	
	@Resource
	private StaffService staffServiceImpl;
	
	//按照条件查询所有的实习生信息
	@RequestMapping("/findAllStaff.do")
	public String findAllStaff(String interviewer,String staff_name,Model model,HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException{
		response.setContentType("text/html;charset=utf-8");
		interviewer = new String(interviewer.getBytes("iso8859-1"),"UTF-8");
		staff_name = new String(staff_name.getBytes("iso8859-1"),"UTF-8");
		String id=request.getParameter("id");		
		if (id!=null) {
			int staff_id=Integer.parseInt(id);
			Staff staff=staffServiceImpl.findOneStaffById(staff_id);
			model.addAttribute("staff", staff);		
		}
		System.out.println(interviewer);
		System.out.println(staff_name);
		List<Staff> staffs=staffServiceImpl.findAllStaff(interviewer, staff_name);
		List<String> interviewers=staffServiceImpl.findAllInterviewer();
		model.addAttribute("staffs", staffs);
		model.addAttribute("interview",interviewer);
		model.addAttribute("staff_name", staff_name);
		model.addAttribute("interviewers", interviewers);
		return "staff/staff";
	}
	//查找具体的实习生信息
	@RequestMapping("/findOneStaffById.do")
	public void findOneStaffById(HttpServletResponse httpServletResponse,HttpServletRequest request) throws IOException{
		httpServletResponse.setContentType("text/html;charset=utf-8");
		int staff_id=Integer.parseInt(request.getParameter("staff_id"));
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		
		System.out.println(staff_id);
		Staff staff=staffServiceImpl.findOneStaffById(staff_id);
		staff.setBrith(format.format(new Date(staff.getBirthdate().getTime())));
		staff.setEntryTime(format.format(new Date(staff.getEntry().getTime())));

		staff.setGraduateTime(format.format(new Date(staff.getGraduate().getTime())));
		PrintWriter out = httpServletResponse.getWriter();
		String data=JSONObject.fromObject(staff).toString();
		out.print(data);
		//model.addAttribute("staff", staff);
		//return "staff/staff";
	}
	//修改实习生信息
	@RequestMapping("/updateStaff.do")
	public String updateStaff(Staff staff,String state,String sex){
		System.out.println("state"+state);
		System.out.println("sex"+sex);
		if ("男".equals(sex)) {
			staff.setStaffSex(1);
		}else{
			staff.setStaffSex(2);
		}
		if ("正常".equals(state)) {
			staff.setStaffState(1);
		}else{
			staff.setStaffState(2);
		}
		System.out.println("zhuangtai"+staff.getStaffState());
		System.out.println("xiangbei"+staff.getStaffSex());
		System.out.println("yuangong"+staff);
		System.out.println("gonghao"+staff.getStaffId());
		System.out.println(staff.getBirthdate());
		staffServiceImpl.updateStaff(staff);
		return "redirect:/staff/findAllStaff.do?interviewer=ALL&staff_name=null";	
	}
	
	//查询学校的分布
	@RequestMapping("/findAllSchool")
	public String findAllSchool(Model model){
		Map<String, Integer> school=staffServiceImpl.findAllSchool();
		int totalstaff=staffServiceImpl.totalstaff();
		System.out.println(school);
		System.out.println("================");
		for (Map.Entry<String, Integer> entry : school.entrySet()) {  
			   String key = entry.getKey().toString();  
			   String value = entry.getValue().toString();  
			   System.out.println("key=" + key + " value=" + value);  
		}
		JSONObject data=JSONObject.fromObject(school);
		model.addAttribute("school", school);
		return "staff/school";
	}
	//查询211人数占比
	@RequestMapping("/findAllTOO.do")
	public String findAllTOO(Model model){
		int too=staffServiceImpl.totalTOO();
		int totalstaff=staffServiceImpl.totalstaff();
		model.addAttribute("too", too);
		model.addAttribute("too2", totalstaff);
		return "staff/school";
		
	}
	//查询专业的分布
	@RequestMapping("/findAllProfessional")
	public String findAllProfessional(Model model){
		Map<String, Integer> professional=staffServiceImpl.findAllProfessional();
		JSONObject data=JSONObject.fromObject(professional);
		model.addAttribute("professional", professional);
		return "staff/school";
	}
	
	//查询男女生占比（男生）
	@RequestMapping("/findStaffBySex")
	public String findStaffBySex(Model model){
		//全部人数
		int totalstaff=staffServiceImpl.totalstaff();
		//男生人数
		int man=staffServiceImpl.totalStaffSexIsMan();
		model.addAttribute("totalstaff", totalstaff);
		model.addAttribute("man", man);
		return "staff/school";
	}
	//查看学历分布
	@RequestMapping("/findAllDegree")
	public String findAllDegree(Model model){
		Map<String, Integer> degree=staffServiceImpl.findAllDegree();
		model.addAttribute("degree", degree);
		return "staff/school";
	}
	//查看淘汰实习生占比
	@RequestMapping("/findStaffStateIsTOO")
	public String findStaffStateIsTOO(Model model){
		int totalStaffStateIsTOO=staffServiceImpl.totalStaffStateIsT();
		int totalStaff=staffServiceImpl.totalstaff();
		model.addAttribute("totalStaffStateIsTOO", totalStaffStateIsTOO);
		model.addAttribute("totalStaff", totalStaff);
		return "staff/school";
	}
	
	//查看部门接纳人数占比
	@RequestMapping("/findStaffByDept")
	public String findStaffByDept(Model model){
		Map<String, Integer> staff_dept=staffServiceImpl.totalStaffByDepartment();
		model.addAttribute("staff_dept", staff_dept);
		ArrayList<String> list1=new ArrayList<String>();
		ArrayList<String> list2=new ArrayList<String>();
		for (Map.Entry<String, Integer> entry : staff_dept.entrySet()) {  
			   String key = entry.getKey();  
			   String value = entry.getValue().toString();  
			   list1.add(key);
			   list2.add(value);
			  } 
			model.addAttribute("department", list1.toString());
			
			model.addAttribute("value", list2);
		return "staff/school";
	}
	
	//导入实习生数据
	@RequestMapping("/importStaff.do")
	public String importStaff(HttpServletRequest request) throws IOException{
		FileUpload upload=new FileUpload();
		String path=upload.fileupload(request);
		System.out.println(path);
		staffServiceImpl.importStaff(path);
		return "redirect:/staff/findAllStaff.do?interviewer=ALL&staff_name=null";	
	}
	
	//跳到echarts页面
	@RequestMapping("/echarts.do")
	public String getEcharts(Model model){
		int too=staffServiceImpl.totalTOO();
		int totalstaff=staffServiceImpl.totalstaff();
		model.addAttribute("too", too);
		model.addAttribute("too2", totalstaff);
		//男生人数
		int man=staffServiceImpl.totalStaffSexIsMan();
		model.addAttribute("man", man);
		int totalStaffStateIsTOO=staffServiceImpl.totalStaffStateIsT();
		model.addAttribute("totalStaffStateIsTOO", totalStaffStateIsTOO);
		Map<String, Integer> staff_dept=staffServiceImpl.totalStaffByDepartment();
		model.addAttribute("staff_dept", staff_dept);
		ArrayList<String> list1=new ArrayList<String>();
		ArrayList<String> list2=new ArrayList<String>();
		for (Map.Entry<String, Integer> entry : staff_dept.entrySet()) {  
			   String key = entry.getKey();  
			   String value = entry.getValue().toString();  
			   list1.add(key);
			   list2.add(value);
			  }
		model.addAttribute("department", list1.toString());
		model.addAttribute("value", list2);
		return "staff/school"; 
	}
}
